package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.StartBundleKey;
import com.zlzxm.shoesmarket.view.activity.manage.bundle.BundleGoodsInfo;
import com.zlzxm.shoesmarket.view.adapter.ADGoodsSizeTag;
import com.zlzxm.shoesmarket.view.adapter.ADSizeMoveDetail;
import com.zlzxm.shoesmarket.view.adapter.entity.EntitySizeMoveDetail;
import com.zlzxm.shoesmarket.view.widget.GoodsInfoView;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;
import com.zlzxm.shoesmarket.view.window.wheelpick.OnOptionSelectListener;
import com.zlzxm.shoesmarket.view.window.wheelpick.WindowWheelPick;
import com.zlzxm.shoesmarket.viewmodel.VMSizeMove;

import java.util.List;

/**
 * CREATE BY zlzxm ON 4/20/21
 * <br>
 *     尺码转移
 */
public class ACSizeMove extends BaseActivity implements OnItemClickListener ,View.OnClickListener{


    public static void start(Context context, BundleGoodsInfo bundleGoodsInfo) {
        Intent starter = new Intent(context, ACSizeMove.class);

        starter.putExtra(StartBundleKey.KEY_GOODS_INFO, bundleGoodsInfo);
        context.startActivity(starter);
    }

    private VMSizeMove vmSizeMove;

    private GoodsInfoView goodsInfoView;

    private RecyclerView rvSize,rvSizeDetail;

    private ADGoodsSizeTag adGoodsSizeTag = new ADGoodsSizeTag();

    private ADSizeMoveDetail adSizeMoveDetail= new ADSizeMoveDetail();

    private TextView tvSure;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_size_move);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("尺码转移");
        simpleHead.setOnItemclicklistener(this::finish);
        goodsInfoView  = ZViewHelp.findById(this,R.id.goodsInfoView);

        rvSize = ZViewHelp.findById(this,R.id.rvSize);

        rvSize.setLayoutManager(new FlexboxLayoutManager(this));

        rvSizeDetail = ZViewHelp.findById(this,R.id.rvSizeDetail);

        rvSizeDetail.setLayoutManager(new LinearLayoutManager(this));

        adGoodsSizeTag.setOnItemClickListener(this);


        tvSure = ZViewHelp.setOnClickListener(this,R.id.tvSure,this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmSizeMove = new ViewModelProvider(this).get(VMSizeMove.class);

        vmSizeMove.getLiveGoodsInfo().observe(this,it ->{

//            adGoodsSizeTag.setList(it.getList());


        });

        vmSizeMove.getLiveSizeDetail().observe(this, it -> adSizeMoveDetail.setList(it));

        vmSizeMove.getLiveRepository().observe(this,this::showPickWindow);
    }

    @Override
    protected void initData() {
        super.initData();
        BundleGoodsInfo bundleGoodsInfo = (BundleGoodsInfo) getIntent().getSerializableExtra(StartBundleKey.KEY_GOODS_INFO);

        updateGoodsInfo(bundleGoodsInfo.getBrand(), bundleGoodsInfo.getCode(),
                bundleGoodsInfo.getName(), bundleGoodsInfo.getFace());
        rvSize.setAdapter(adGoodsSizeTag);
        rvSizeDetail.setAdapter(adSizeMoveDetail);
        vmSizeMove.requestInfo();
        vmSizeMove.requestSize();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

        adGoodsSizeTag.setSelectPosition(position);
    }

    private void updateGoodsInfo(String brand,String code,String name,String face){

        goodsInfoView.updateBrand(brand);
        goodsInfoView.updateCode(code);
        goodsInfoView.updateName(name);
        goodsInfoView.updateFace(face);


    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.tvSure){

            sure();
        }

    }


    private void sure(){

        vmSizeMove.requestRepository();


    }


    private void showPickWindow(List<EntityOption> list){
        WindowWheelPick.getInstance()
                .setOptions(list)
                .setOnOptionSelectListener(new OnOptionSelectListener() {
                    @Override
                    public void onSelected(EntityOption entityOption) {

                    }
                })
                .show(getSupportFragmentManager(),"");

    }

}
